package com.mlnx.service.base.config;

import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;
import org.shan.spring.cache.config.KeyConfig;

/**
 * Created by amanda.shan on 2019/10/25.
 */
public class MutilDeviceCacheConfig {

    class SupportL1Config implements KeyConfig {

        private String key;
        private Integer size;
        private int ttl = -1;   // 不设置超时时间 单位是s

        public SupportL1Config(String key, Integer size) {
            this.key = key;
            this.size = size;
        }

        public SupportL1Config(String key, Integer size, int ttl) {
            this.key = key;
            this.size = size;
            this.ttl = ttl;
        }

        @Override
        public CacheConfigurationBuilder<String, Object> getEhcacheBuilder() {

            if (size > 0) {
                // 限制缓存最大数量
                return cacheConfigurationBuilder(ResourcePoolsBuilder.heap(size));
            } else {
                // 不限制数量
                return cacheConfigurationBuilder(ResourcePoolsBuilder.heap(Integer.MAX_VALUE));
            }
        }

        @Override
        public String getKey() {
            return key;
        }

        @Override
        public boolean supportL2() {
            return false;
        }

        @Override
        public int timeToL2LiveInSeconds() {
            return ttl;
        }
    }

}
